loadstring(exports.dgs:dgsImportFunction())()

local sw,sh = guiGetScreenSize()
local px,py = sw/1366,sh/768
font = dgsCreateFont( ":cmta-pobieraniezasobow/OSB.ttf", 12*px )
font2 = dgsCreateFont( ":cmta-pobieraniezasobow/OSB.ttf", 18*px )
local ped = createPed(111,2657.55,-2030.73,13.55,1)
setElementRotation(ped,0,0,180)
setElementFrozen(ped,true)

cmtaPANEsklepubroni = false
cmtaDialogsklepubroni = false
page = 1

playerDialogue={}
playerBlockDialogue={}

dialogsWeaponShop ={
    [0] = {
        wshop = "";
        player = "";
    };
    [1] = {
        wshop = "Siema, czego potrzeba?";
        player = "Witam";
    };
    [2] = {
        wshop = "Jasne, tylko długo się nie zastanawiaj";
        player = "Pokaż mi swój towar";
    };
}

weapons = {
    [4] = {
        reqlvl = 7;
        price = 350;
        amount = 1;
        text = "#DD6B20Nóż\n#808080Wymagany poziom: #ff28287\n#808080Cena: #ff2828350 pkt";
    };
    [5] = {
        reqlvl = 7;
        price = 350;
        amount = 1;
        text = "#DD6B20Kij bejsbolowy\n#808080Wymagany poziom: #ff28287\n#808080Cena: #ff2828350 pkt";
    };
    [18] = {
        reqlvl = 23;
        price = 900;
        amount = 5;
        text = "#DD6B20Molotov\n#808080Wymagany poziom: #ff282823\n#808080Ilość: #ff28285 sztuk\n#808080Cena: #ff2828900 pkt";
    };  
    [22] = {
        reqlvl = 15;
        price = 200;
        amount = 25;
        text = "#DD6B20Colt-45\n#808080Wymagany poziom: #ff282815\n#808080Amunicja: #ff282825 sztuk\n#808080Cena: #ff2828200 pkt";
    };
    [23] = {
        reqlvl = 20;
        price = 250;
        amount = 25;
        text = "#DD6B20Silenced\n#808080Wymagany poziom: #ff282820\n#808080Amunicja: #ff282825 sztuk\n#808080Cena: #ff2828250 pkt";

    };
    [24] = {
        reqlvl = 35;
        price = 400;
        amount = 20;
        text = "#DD6B20Deagle\n#808080Wymagany poziom: #ff282835\n#808080Amunicja: #ff282820 sztuk\n#808080Cena: #ff2828400 pkt";
    };
    [25] = {
        reqlvl = 33;
        price = 550;
        amount = 20;
        text = "#DD6B20Shotgun\n#808080Wymagany poziom: #ff282833\n#808080Amunicja: #ff282820 sztuk\n#808080Cena: #ff2828550 pkt";
    };
    [26] = {
        reqlvl = 38;
        price = 680;
        amount = 15;
        text = "#DD6B20Sawed-off\n#808080Wymagany poziom: #ff282838\n#808080Amunicja: #ff282815 sztuk\n#808080Cena: #ff2828680 pkt";
    };
    [27] = {
        reqlvl = 42;
        price = 765;
        amount = 12;
        text = "#DD6B20Combat Shotgun\n#808080Wymagany poziom: #ff282842\n#808080Amunicja: #ff282812 sztuk\n#808080Cena: #ff2828765 pkt";
    };
    [28] = {
        reqlvl = 46;
        price = 550;
        amount = 40;
        text = "#DD6B20Uzi\n#808080Wymagany poziom: #ff282846\n#808080Amunicja: #ff282840 sztuk\n#808080Cena: #ff2828550 pkt";
    };
    [29] = {
        reqlvl = 49;
        price = 625;
        amount = 30;
        text = "#DD6B20MP5\n#808080Wymagany poziom: #ff282849\n#808080Amunicja: #ff282830 sztuk\n#808080Cena: #ff2828625 pkt";
    };
    [32] = {
        reqlvl = 46;
        price = 550;
        amount = 40;
        text = "#DD6B20Tec-9\n#808080Wymagany poziom: #ff282846\n#808080Amunicja: #ff282840 sztuk\n#808080Cena: #ff2828550 pkt";
    };
    [30] = {
        reqlvl = 65;
        price = 910;
        amount = 50;
        text = "#DD6B20AK-47\n#808080Wymagany poziom: #ff282865\n#808080Amunicja: #ff282850 sztuk\n#808080Cena: #ff2828910 pkt";
    };   
    [31] = {
        reqlvl = 75;
        price = 950;
        amount = 50;
        text = "#DD6B20M4\n#808080Wymagany poziom: #ff282875\n#808080Amunicja: #ff282850 sztuk\n#808080Cena: #ff2828950 pkt";
    };  
    [33] = {
        reqlvl = 44;
        price = 600;
        amount = 20;
        text = "#DD6B20Rifle\n#808080Wymagany poziom: #ff282844\n#808080Amunicja: #ff282820 sztuk\n#808080Cena: #ff2828600 pkt";
    };  
    [34] = {
        reqlvl = 115;
        price = 1500;
        amount = 10;
        text = "#DD6B20Sniper\n#808080Wymagany poziom: #ff2828115\n#808080Amunicja: #ff282810 sztuk\n#808080Cena: #ff28281500 pkt";
    };  
    [35] = {
        reqlvl = 170;
        price = 2700;
        amount = 1;
        text = "#DD6B20Rocket Launcher\n#808080Wymagany poziom: #ff2828170\n#808080Amunicja: #ff28281 sztuka\n#808080Cena: #ff28282700 pkt";
    };  
    [16] = {
        reqlvl = 105;
        price = 1000;
        amount = 1;
        text = "#DD6B20Grenade\n#808080Wymagany poziom: #ff2828105\n#808080Amunicja: #ff28281 sztuka\n#808080Cena: #ff28281000 pkt";
    };  
}

addEvent("cmta_sklepbron:showGUI", true)
addEventHandler("cmta_sklepbron:showGUI", resourceRoot, function()
    dialog1 = dgsCreateImage(1300*px,460*py,50*px,50*py,":cmta-diler/images/button1.png",false,_,tocolor(255,255,255,255))
    dialog2 = dgsCreateImage(1300*px,530*py,50*px,50*py,":cmta-diler/images/button2.png",false,_,tocolor(255,255,255,255))
    textbutton1 = dgsCreateLabel(1230*px,460*py,50*px,50*py,"Pokaż mi swój towar",false,false)
    dgsSetFont( textbutton1, font )
    dgsSetProperty(textbutton1,"colorcoded",true)
    dgsSetProperty(textbutton1,"alignment",{"right","center"})
	textbutton2 = dgsCreateLabel(1230*px,530*py,50*px,50*py,"Odejdź",false,false)
    dgsSetFont( textbutton2, font )
    dgsSetProperty(textbutton2,"colorcoded",true)
    dgsSetProperty(textbutton2,"alignment",{"right","center"})
    local x,y,z = getElementPosition(localPlayer)
    text3d = dgsCreate3DText(-1973.0452880859,101,27.8+1.2,"",tocolor(153,50,204,255),false,0.4,0.4,10)
    text3d2 = dgsCreate3DText(x,y,z+1.2,"",white,false,0.35,0.35,10)
    dgs3DTextAttachToElement(text3d,ped,0,0,1)
    dgs3DTextAttachToElement(text3d2,localPlayer,0,0,1)
    cmtaDialogsklepubroni = true
    playerDialogue[localPlayer] = 1
    refreshtext()
    setPedFrozen(localPlayer,true)
    setElementRotation(localPlayer,0,0,0)
    toggleControl("forwards",false)
    toggleControl("backwards",false)
    toggleControl("left",false)
    toggleControl("right",false)
    addEventHandler( "onClientKey", getRootElement(), onkey) 
end)

function creategui()
    rndRect = dgsCreateRoundRect(0,false,tocolor(30,30,30,230))  
    rect = dgsCreateImage(350*px,150*py,666*px,468*py,rndRect,false)
    text = dgsCreateLabel(0*px,20*py,666*px,50*py,"#DD6B20Sklep z bronią i amunicją",false,rect)
    dgsSetFont( text, font2 )
    dgsSetProperty(text,"colorcoded",true)
    dgsSetProperty(text,"alignment",{"center","top"})
    buybutton1 = dgsCreateButton( 70*px, 320*py, 128*px, 30*py, "Kup", false,rect,false,false,false,false,false,false,tocolor(90,90,90),tocolor(221,107,32),tocolor(221,107,32))
    buybutton2 = dgsCreateButton( 270*px, 320*py, 128*px, 30*py, "Kup", false,rect,false,false,false,false,false,false,tocolor(90,90,90),tocolor(221,107,32),tocolor(221,107,32))
    buybutton3 = dgsCreateButton( 470*px, 320*py, 128*px, 30*py, "Kup", false,rect,false,false,false,false,false,false,tocolor(90,90,90),tocolor(221,107,32),tocolor(221,107,32))
    buttonexit = dgsCreateButton( 630*px, 20*py, 20*px, 20*py, "✖", false,rect,false,false,false,false,false,false,tocolor(90,90,90),tocolor(255,0,0),tocolor(255,0,0))
    button1 = dgsCreateButton( 230*px, 420*py, 30*px, 30*py, " 1", false,rect,false,false,false,false,false,false,tocolor(221,107,32),tocolor(221,107,32),tocolor(221,107,32))
    button2 = dgsCreateButton( 265*px, 420*py, 30*px, 30*py, " 2", false,rect,false,false,false,false,false,false,false,tocolor(90,90,90),tocolor(221,107,32))
    button3 = dgsCreateButton( 300*px, 420*py, 30*px, 30*py, " 3", false,rect,false,false,false,false,false,false,false,tocolor(90,90,90),tocolor(221,107,32))
    button4 = dgsCreateButton( 336*px, 420*py, 30*px, 30*py, " 4", false,rect,false,false,false,false,false,false,false,tocolor(90,90,90),tocolor(221,107,32))
    button5 = dgsCreateButton( 371*px, 420*py, 30*px, 30*py, " 5", false,rect,false,false,false,false,false,false,false,tocolor(90,90,90),tocolor(221,107,32))
    button6 = dgsCreateButton( 406*px, 420*py, 30*px, 30*py, " 6", false,rect,false,false,false,false,false,false,false,tocolor(90,90,90),tocolor(221,107,32))
    image1 = dgsCreateImage(70*px,80*py,128*px,128*py,"img/4.png",false,rect,tocolor(255,255,255,255))
    image2 = dgsCreateImage(270*px,80*py,128*px,128*py,"img/5.png",false,rect,tocolor(255,255,255,255))
    image3 = dgsCreateImage(470*px,80*py,128*px,128*py,"img/18.png",false,rect,tocolor(255,255,255,255))
    textimg1 = dgsCreateLabel(70*px,230*py,128*px,50*py,""..weapons[4].text.."",false,rect)
    textimg2 = dgsCreateLabel(270*px,230*py,128*px,50*py,""..weapons[5].text.."",false,rect)
    textimg3 = dgsCreateLabel(470*px,230*py,128*px,50*py,""..weapons[18].text.."",false,rect)
    dgsSetFont( textimg1, font )
    dgsSetProperty(textimg1,"colorcoded",true)
    dgsSetProperty(textimg1,"alignment",{"center","top"})
    dgsSetFont( textimg2, font )
    dgsSetProperty(textimg2,"colorcoded",true)
    dgsSetProperty(textimg2,"alignment",{"center","top"})
    dgsSetFont( textimg3, font )
    dgsSetProperty(textimg3,"colorcoded",true)
    dgsSetProperty(textimg3,"alignment",{"center","top"})
    cmtaPANEsklepubroni = true
    databutton1 = {7,350,1}
    databutton2 = {7,350,1}
    databutton3 = {23,900,5}
    showCursor(true)
    addEventHandler ( "onDgsMouseClick", getRootElement(), onclick)
end

addEvent("cmta_sklepbron:hideGUI", true)
addEventHandler("cmta_sklepbron:hideGUI", resourceRoot, function()
    removeEventHandler ( "onDgsMouseClick", getRootElement(), onclick)
    if isElement(rndRect) then
    destroyElement(rndRect)
    destroyElement(rect)
    end
    setPedFrozen(localPlayer,false)
    toggleControl("forwards",true)
    toggleControl("backwards",true)
    toggleControl("left",true)
    toggleControl("right",true)
    showCursor(false)
    cmtaPANEsklepubroni = false
end)


function onclick ( btn, state )
    if btn == "left" and state == "down" and cmtaPANEsklepubroni == true then
        dgsBringToFront(buttonexit,true)
        if source == buttonexit then
            removeEventHandler ( "onDgsMouseClick", getRootElement(), onclick)
            destroyElement(rndRect)
            destroyElement(rect)
            setPedFrozen(localPlayer,false)
            toggleControl("forwards",true)
            toggleControl("backwards",true)
            toggleControl("left",true)
            toggleControl("right",true)
            showCursor(false)
            cmtaPANEsklepubroni = false
        end
        if source == button1 or source == button2 or source == button3 or source == button4 or source == button5 or source == button6 or source == button7 then 
        refreshcolors()
        end
        if source == button1 then
            dgsSetProperty(button1,"color",{tocolor(221,107,32),tocolor(221,107,32),tocolor(221,107,32)})
            dgsImageSetImage(image1,"img/4.png")
            dgsImageSetImage(image2,"img/5.png")
            dgsImageSetImage(image3,"img/18.png")
            dgsSetText( textimg1, weapons[4].text )
            dgsSetText( textimg2, weapons[5].text )
            dgsSetText( textimg3, weapons[18].text )
            databutton1 = {7,350,1}
            databutton2 = {7,350,1}
            databutton3 = {23,900,5}
            page = 1
        elseif source == button2 then
            dgsSetProperty(button2,"color",{tocolor(221,107,32),tocolor(221,107,32),tocolor(221,107,32)})
            dgsImageSetImage(image1,"img/22.png")
            dgsImageSetImage(image2,"img/23.png")
            dgsImageSetImage(image3,"img/24.png")
            dgsSetText( textimg1, weapons[22].text )
            dgsSetText( textimg2, weapons[23].text )
            dgsSetText( textimg3, weapons[24].text )
            databutton1 = {15,200,25}
            databutton2 = {20,250,25}
            databutton3 = {35,400,20}
            page = 2
        elseif source == button3 then
            dgsSetProperty(button3,"color",{tocolor(221,107,32),tocolor(221,107,32),tocolor(221,107,32)})
            dgsImageSetImage(image1,"img/25.png")
            dgsImageSetImage(image2,"img/26.png")
            dgsImageSetImage(image3,"img/27.png")
            dgsSetText( textimg1, weapons[25].text )
            dgsSetText( textimg2, weapons[26].text )
            dgsSetText( textimg3, weapons[27].text )
            page = 3
        elseif source == button4 then
            dgsSetProperty(button4,"color",{tocolor(221,107,32),tocolor(221,107,32),tocolor(221,107,32)})
            dgsImageSetImage(image1,"img/28.png")
            dgsImageSetImage(image2,"img/29.png")
            dgsImageSetImage(image3,"img/32.png")
            dgsSetText( textimg1, weapons[28].text )
            dgsSetText( textimg2, weapons[29].text )
            dgsSetText( textimg3, weapons[32].text )
            page = 4
        elseif source == button5 then
            dgsSetProperty(button5,"color",{tocolor(221,107,32),tocolor(221,107,32),tocolor(221,107,32)})
            dgsImageSetImage(image1,"img/30.png")
            dgsImageSetImage(image2,"img/31.png")
            dgsImageSetImage(image3,"img/33.png")
            dgsSetText( textimg1, weapons[30].text )
            dgsSetText( textimg2, weapons[31].text )
            dgsSetText( textimg3, weapons[33].text )
            page = 5
        elseif source == button6 then
            dgsSetProperty(button6,"color",{tocolor(221,107,32),tocolor(221,107,32),tocolor(221,107,32)})
            dgsImageSetImage(image1,"img/34.png")
            dgsImageSetImage(image2,"img/35.png")
            dgsImageSetImage(image3,"img/16.png")
            dgsSetText( textimg1, weapons[34].text )
            dgsSetText( textimg2, weapons[35].text )
            dgsSetText( textimg3, weapons[16].text )
            page = 6
        end
        if source == buybutton1 then
            if databutton1[1] > getElementData(localPlayer,"player:data").lvl then
                outputChatBox("nie masz levela mordo")
            end
            if databutton1[2] > getElementData(localPlayer,"player:data").points then
                outputChatBox("nie masz punktów mordo")
            end            
        end
    end
end

function onkey(button,press)
    if cmtaDialogsklepubroni and press == true then
    if playerBlockDialogue[localPlayer] == 1 then return end
    if playerDialogue[localPlayer] == 1 and button == "2" then
       playerDialogue[localPlayer] = 0
       setPedFrozen(localPlayer,false)
        toggleControl("forwards",true)
        toggleControl("backwards",true)
        toggleControl("left",true)
        toggleControl("right",true)
        destroyElement(text3d)
        destroyElement(text3d2)
        destroyElement(dialog1)
        destroyElement(dialog2)
        destroyElement(textbutton1)
        destroyElement(textbutton2)
        cmtaDialogsklepubroni = false
        removeEventHandler( "onClientKey", getRootElement(), onkey) 
    end
    if playerDialogue[localPlayer] == 1 and button == "1" then
        playerDialogue[localPlayer] = 2
        refreshtext()
        destroyElement(dialog1)
        destroyElement(dialog2)
        destroyElement(textbutton1)
        destroyElement(textbutton2) 
        removeEventHandler( "onClientKey", getRootElement(), onkey) 
    end
end
end

function refreshtext()
    dgsSetText(text3d2,dialogsWeaponShop[playerDialogue[localPlayer]].player)
    dgsSetText(text3d,"") 
    playerBlockDialogue[localPlayer] = 1
    setTimer(function()
        if isElement(text3d) then
        dgsSetText(text3d,dialogsWeaponShop[playerDialogue[localPlayer]].wshop) 
        playerBlockDialogue[localPlayer] = 0
        if playerDialogue[localPlayer] == 2 then
        setTimer(function()
            dgsSetText(text3d2,"")
            dgsSetText(text3d,"")         
        creategui()
        end,2000,1)
        end
        end
    end,2000,1)
end

function refreshcolors()
    dgsSetProperty(button1,"color",{tocolor(90,90,90),tocolor(221,107,32)})
    dgsSetProperty(button2,"color",{tocolor(90,90,90),tocolor(221,107,32)})
    dgsSetProperty(button3,"color",{tocolor(90,90,90),tocolor(221,107,32)})
    dgsSetProperty(button4,"color",{tocolor(90,90,90),tocolor(221,107,32)})
    dgsSetProperty(button5,"color",{tocolor(90,90,90),tocolor(221,107,32)})
    dgsSetProperty(button6,"color",{tocolor(90,90,90),tocolor(221,107,32)})
end

